package bg.btanev.eightyfour.client.view;

import bg.btanev.eightyfour.client.presenter.PersonPresenter.PersonDisplay;

import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CaptionPanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import eu.maydu.gwt.validation.client.DefaultValidationProcessor;
import eu.maydu.gwt.validation.client.ValidationProcessor;
import eu.maydu.gwt.validation.client.actions.StyleAction;
import eu.maydu.gwt.validation.client.validators.standard.NotEmptyValidator;
import eu.maydu.gwt.validation.client.validators.strings.EmailValidator;

/**
 *
 * @author B.Tanev
 *
 */
public class PersonView extends Composite implements PersonDisplay {
	private final TextBox firstNameData;
	private final TextBox surnameData;
	private final TextBox lastNameData;
	private final TextBox emailData;
	private final TextBox telephoneData;
	private final TextBox websiteData;
	private final TextBox countryData;
	private final TextBox cityData;
	private final TextBox streetData;
	private final Button savePersonButton;
	private final ValidationProcessor validator;

	public PersonView() {
		final VerticalPanel personViewContainer = new VerticalPanel();
		initWidget(personViewContainer);
		personViewContainer.setSize("550px", "430px");

		final CaptionPanel personNamePanel = new CaptionPanel("Name");
		personViewContainer.add(personNamePanel);
		personNamePanel.setWidth("100%");

		final Grid personContainer = new Grid(3, 2);
		personContainer.setCellSpacing(5);
		personNamePanel.setContentWidget(personContainer);
		personContainer.setWidth("100%");

		final Label nameCaption = new Label("Name");
		personContainer.setWidget(0, 0, nameCaption);
		nameCaption.setWidth("200px");
		nameCaption.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);

		firstNameData = new TextBox();
		firstNameData.setMaxLength(50);
		personContainer.setWidget(0, 1, firstNameData);
		firstNameData.setWidth("300px");

		final Label surnameCaption = new Label("Surname");
		surnameCaption.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		personContainer.setWidget(1, 0, surnameCaption);
		surnameCaption.setWidth("200px");

		surnameData = new TextBox();
		surnameData.setMaxLength(50);
		personContainer.setWidget(1, 1, surnameData);
		surnameData.setWidth("300px");

		final Label lastCaption = new Label("Last");
		lastCaption.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		personContainer.setWidget(2, 0, lastCaption);
		lastCaption.setWidth("200px");

		lastNameData = new TextBox();
		lastNameData.setMaxLength(50);
		personContainer.setWidget(2, 1, lastNameData);
		lastNameData.setWidth("300px");

		final CaptionPanel contactInfoPanel = new CaptionPanel("Contact Info");
		personViewContainer.add(contactInfoPanel);
		contactInfoPanel.setWidth("100%");

		final VerticalPanel verticalPanel = new VerticalPanel();
		verticalPanel.setSpacing(5);
		contactInfoPanel.setContentWidget(verticalPanel);
		verticalPanel.setWidth("100%");

		final Grid contactInfoContainer = new Grid(3, 2);
		contactInfoContainer.setCellSpacing(5);
		verticalPanel.add(contactInfoContainer);
		contactInfoContainer.setWidth("100%");

		final CaptionPanel addressPanel = new CaptionPanel("Address");
		verticalPanel.add(addressPanel);
		addressPanel.setWidth("540px");

		final Grid addressContainer = new Grid(3, 2);
		addressContainer.setCellSpacing(5);
		addressPanel.setContentWidget(addressContainer);
		addressContainer.setWidth("100%");

		final Label countryCaption = new Label("Country");
		addressContainer.setWidget(0, 0, countryCaption);
		countryCaption.setWidth("200px");

		countryData = new TextBox();
		countryData.setMaxLength(50);
		addressContainer.setWidget(0, 1, countryData);
		countryData.setWidth("300px");

		final Label cityCaption = new Label("City");
		addressContainer.setWidget(1, 0, cityCaption);
		cityCaption.setWidth("200px");

		cityData = new TextBox();
		cityData.setMaxLength(50);
		addressContainer.setWidget(1, 1, cityData);
		cityData.setWidth("300px");

		final Label streetCaption = new Label("Street");
		addressContainer.setWidget(2, 0, streetCaption);
		streetCaption.setWidth("200px");

		streetData = new TextBox();
		streetData.setMaxLength(50);
		addressContainer.setWidget(2, 1, streetData);
		streetData.setWidth("300px");

		final Label emailCaption = new Label("E-Mail");
		contactInfoContainer.setWidget(0, 0, emailCaption);
		emailCaption.setWidth("200px");

		emailData = new TextBox();
		emailData.setMaxLength(100);
		contactInfoContainer.setWidget(0, 1, emailData);
		emailData.setWidth("300px");

		final Label telephoneCaption = new Label("Telephone");
		contactInfoContainer.setWidget(1, 0, telephoneCaption);
		telephoneCaption.setWidth("200px");

		telephoneData = new TextBox();
		telephoneData.setMaxLength(20);
		contactInfoContainer.setWidget(1, 1, telephoneData);
		telephoneData.setWidth("300px");

		final Label websiteCaption = new Label("Website");
		contactInfoContainer.setWidget(2, 0, websiteCaption);
		websiteCaption.setWidth("200px");

		websiteData = new TextBox();
		websiteData.setMaxLength(50);
		contactInfoContainer.setWidget(2, 1, websiteData);
		websiteData.setWidth("300px");

		final HorizontalPanel buttonsPanel = new HorizontalPanel();
		buttonsPanel.setSpacing(10);
		buttonsPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		personViewContainer.add(buttonsPanel);
		buttonsPanel.setWidth("100%");

		savePersonButton = new Button("New button");
		savePersonButton.setText("Save");
		buttonsPanel.add(savePersonButton);
		savePersonButton.setWidth("67px");

		// final CaptionPanel experiencePanel = CreateExperiencePanels();
		// personViewContainer.add(experiencePanel);

		validator = new DefaultValidationProcessor();

		validator.addValidators("personValidation",
				new NotEmptyValidator(firstNameData)
					.addActionForFailure(new StyleAction("validationFailedBorder")),
				new NotEmptyValidator(lastNameData)
					.addActionForFailure(new StyleAction("validationFailedBorder")),
				new EmailValidator(emailData)
					.addActionForFailure(new StyleAction("validationFailedBorder"))
				);
	}

	private CaptionPanel CreateExperiencePanels() {
		final CaptionPanel experiencePanel = new CaptionPanel("Experience");
		experiencePanel.setSize("100%", "300px");
		experiencePanel.setVisible(false);

		final HorizontalPanel experienceContainer = new HorizontalPanel();
		experiencePanel.setContentWidget(experienceContainer);
		experienceContainer.setSpacing(3);
		experienceContainer.setSize("100%", "100%");

		final CaptionPanel workExperiencePanel = new CaptionPanel("Work");
		experienceContainer.add(workExperiencePanel);
		workExperiencePanel.setSize("320px", "98%");

		final CaptionPanel educationExperiencePanel = new CaptionPanel("Education");
		experienceContainer.add(educationExperiencePanel);
		educationExperiencePanel.setSize("320px", "98%");
		return experiencePanel;
	}

	public HasText getFirstNameData() {
		return firstNameData;
	}

	public HasText getSurnameData() {
		return surnameData;
	}

	public HasText getLastNameData() {
		return lastNameData;
	}

	public HasText getEmailData() {
		return emailData;
	}

	public HasText getTelephoneData() {
		return telephoneData;
	}

	public HasText getWebsiteData() {
		return websiteData;
	}

	public HasText getCountryData() {
		return countryData;
	}

	public HasText getCityData() {
		return cityData;
	}

	public HasText getStreetData() {
		return streetData;
	}

	public HasClickHandlers getSaveClickHandlers() {
		return savePersonButton;
	}

	public boolean isValid() {
		return validator.validate("personValidation");
	}

	@Override
	public Widget asWidget() {
		return this;
	}
}
